package yamen.bdwm.data;

import java.util.ArrayList;

import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.WmBoardItem;
import yamen.bdwm.datastruct.WmDataAdapter;
import yamen.bdwm.datastruct.WmList;
import android.os.AsyncTask;

public class FavouriteList extends WmList {

	public void getRefreshed(final onListRefreshedListener mListener) {

		new AsyncTask<Void, Void, Integer>() {

			@Override
			protected Integer doInBackground(Void... params) {
				ArrayList<WmBoardItem> wms = null;
				if (!WmMyData.getinstance().parser.is_login()) {
					return -1;
				}
				wms = WmMyData.getinstance().parser.getFavoriteBoards();

				if (wms == null)
					return -1;
				int a = 0;
				FavouriteList.this.list.clear();
				for (int i = 0; i < wms.size(); i++) {
					a += FavouriteList.this.Add(wms.get(i));
				}
				return new Integer(a);
			}

			protected void onPostExecute(Integer i) {
				if (i >= 0) {
					FavouriteList.this.NotifyAllAdapter();
					if (mListener != null)
						mListener.onRefreshed(i);
				} else {
					if (mListener != null)
						mListener.onError(-1);
				}
			}
		}.execute();
	}

	public void CreateAdapter(int tag, int resource) {
		if (!adapterList.containsKey(tag)) {
			WmDataAdapter adapter = new WmDataAdapter(resource, this);
			adapterList.put(tag, adapter);
		}
	}
}
